package com.oto.web.resource;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.xml.bind.JAXBElement;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.oto.Constants;
import com.oto.model.FlowDO;
import com.oto.util.JAXBUtils;
import com.oto.web.model.Message;
import com.oto.web.model.ObjectFactory;
import com.oto.web.model.XmlFlow;
import com.oto.web.model.XmlPack;

/**
 * @author xueqiang.mi
 * @since 2011-1-15
 */
@Path("/searchFrids")
@Component
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class SearchFridsResource extends BaseResource {

	@GET
	@Produces("application/xml")
	public String search(@QueryParam(value = "key") String key) {
		messageService.store("processing keywords:" + key);
		List<FlowDO> flows = new ArrayList<FlowDO>();
		ObjectFactory objectFactory = new ObjectFactory();
		XmlPack xPack = objectFactory.createPack();
		xPack.setName(key);
		for (FlowDO f : flows) {
			XmlFlow xFlow = objectFactory.createFlow();
			xFlow.setName(f.getName());
			xFlow.setContent(f.getContent());
			xFlow.setType(f.getType());
			xPack.getFlows().add(xFlow);
		}

		Message msg = objectFactory.createMessage();
		msg.setPack(xPack);
		JAXBElement<Message> je = objectFactory.createMessage(msg);
		StringWriter writer = new StringWriter();
		JAXBUtils.marshal(Constants.JAXB_CONTEXT_PATH, je, writer);
		return writer.toString();
	}
}
